﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//using SynapseGaming.LightingSystem.Rendering;

namespace The_LarX
{
    public class PickingTriangle : Picking
    {
        public Viewport Viewport;

        public PickingTriangle()
        {
            Viewport = Engine.GraphicsDevice.Viewport;
        }

        public override Actor Pick(int x, int y, bool Manipulator)
        {
            Ray ray = CalculateCursorRay(Viewport, new Vector2(x, y));

            float closestIntersection = float.MaxValue;

            return null;
        }

        /*private float? RayIntersectsSceneObject(Ray ray, SceneObject scene)
        {
            return null;
            //Matrix inverseTransform = Matrix.Invert(modelTransform);

            //ray.Position = Vector3.Transform(ray.Position, inverseTransform);
            //ray.Direction = Vector3.TransformNormal(ray.Direction, inverseTransform);

            if (scene.WorldBoundingSphere.Intersects(ray) == null)
            {
                return null;
            }
            else
            {
                float? closestIntersection = null;

                Vector3[] data;
                scene.RenderableMeshes[0].VertexBuffer.GetData<Vector3>(data);
                /*

                for (int i = 0; i < vertices.Length; i += 3)
                {
                }*/
         /*   }
        }*/

        private Ray CalculateCursorRay(Viewport viewport, Vector2 Position)
        {
            Vector3 nearSource = new Vector3(Position, 0f);
            Vector3 farSource = new Vector3(Position, 1f);

            Vector3 nearPoint = viewport.Unproject(nearSource,
                Camera.Projection, Camera.View, Matrix.Identity);

            Vector3 farPoint = viewport.Unproject(farSource,
                Camera.Projection, Camera.View, Matrix.Identity);

            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            return new Ray(nearPoint, direction);
        }
    }
}
